package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Override
    public PageResponse<NursingProjectVo> page(Integer pageNum, Integer pageSize, String name, Integer status) {
        PageHelper.startPage(pageNum,pageSize);
        List<NursingProjectVo> nursingProjectList = nursingProjectMapper.page(name,status);

        //这里不能够对重新复制属性值之后的NursingProjectVo作为封装泛型，规定只能够以分页的结果是哪个就要用哪个
        //所以用NursingProject可以,NursingProjectVo不行
        // List<NursingProjectVo> nursingProjectVoList = new ArrayList<>();
        // for (NursingProject nursingProject : nursingProjectList) {
        //     NursingProjectVo nursingProjectVo = new NursingProjectVo();
        //     BeanUtil.copyProperties(nursingProject,nursingProjectVo);
        //     nursingProjectVoList.add(nursingProjectVo);
        // }

        Page<NursingProjectVo> page = (Page<NursingProjectVo>) nursingProjectList;

        //方式1：传统方式，一个个set
        // PageResponse pageResponse = new PageResponse();
        // pageResponse.setTotal(page.getTotal());
        // pageResponse.setPageSize(page.getPageSize());
        // pageResponse.setRecords(page.getResult());
        // pageResponse.setPage(pageNum);
        // pageResponse.setPages(new Long(page.getPages()));

        //方式2：建造者模式，前提是该类上有@builder注解，该注解是由lombok提供的
        // return PageResponse.<NursingProjectVo>builder()
        //         .page(pageNum)
        //         .total(page.getTotal())
        //         .pages(new Long(page.getPages()))
        //         .pageSize(page.getPageSize())
        //         .records(page.getResult())
        //         .build();

        //方法3：使用架构师封装好的of方法，本质也是建造者模式
        //第一个参数是封装好的page对象，第二个参数是分页的数据类型不要List
        return PageResponse.of(page, NursingProjectVo.class);
    }

    @Override
    public List<NursingProjectVo> getAll() {
        List<NursingProjectVo> list = nursingProjectMapper.getAll();
        return list;
    }

    @Override
    public void add(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = new NursingProject();
        BeanUtil.copyProperties(nursingProjectDto,nursingProject);
        nursingProjectMapper.add(nursingProject);
    }

    @Override
    public NursingProjectVo getById(Long id) {
        NursingProject nursingProject = nursingProjectMapper.getById(id);
        NursingProjectVo nursingProjectVo = new NursingProjectVo();
        BeanUtil.copyProperties(nursingProject,nursingProjectVo);
        return nursingProjectVo;
    }

    @Override
    public void deleteById(Long id) {
        nursingProjectMapper.deleteById(id);
    }

    @Override
    public void updateStatus(Long id, int status) {
        nursingProjectMapper.updateStatus(id,status);
    }

    @Override
    public void update(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = new NursingProject();
        BeanUtil.copyProperties(nursingProjectDto,nursingProject);
        nursingProjectMapper.update(nursingProject);
    }
}
